import request from "@/utils/request";
import {RoleQuery, RolePageResult, RoleForm} from "./model";

class RoleAPI {
    /**
     * 通过用户id获取角色id
     * @param userId
     */
    static getRoleByUser(userId: number) {
        return request<any, ApiResult>({
            url: "/api/sysUserRole/getRoleByUser",
            method: "post",
            data: {userId: userId},
        });
    }

    /**
     * 获取角色分页数据
     *
     * @param queryParams
     */
    static getPage(queryParams?: RoleQuery) {
        return request<any, ApiResult>({
            url: "/api/sysRole/listPage",
            method: "post",
            data: queryParams,
        });
    }

    /**
     * 获取角色下拉数据源
     *
     * @param queryParams
     */
    static getOptions(queryParams?: RoleQuery) {
        return request<any, ApiResult>({
            url: "/api/sysRole/getAllRole",
            method: "post",
            data: queryParams,
        });
    }

    /**
     * 获取角色的菜单ID集合
     *
     * @param queryParams
     */
    static getRoleMenuIds(roleId: number) {
        return request<any, ApiResult>({
            url: "/api/sysRoleMenu/getMenusByRoleId",
            method: "post",
            data: {roleId: roleId},
        });
    }

    /**
     * 分配菜单权限给角色
     *
     * @param queryParams
     */
    static updateRoleMenus(roleId: number, data: number[]) {
        return request({
            url: "/api/sysRoleMenu/updateMenusByRoleId",
            method: "post",
            data: {roleId: roleId, menuIds: data.toString()},
        });
    }


    /**
     * 添加角色
     *
     * @param data
     */
    static add(data: RoleForm) {
        return request({
            url: "/api/sysRole/add",
            method: "post",
            data: data,
        });
    }

    /**
     * 更新角色
     *
     * @param id
     * @param data
     */
    static update(data: RoleForm) {
        return request({
            url: "/api/sysRole/update",
            method: "post",
            data: data,
        });
    }

    /**
     * 批量删除角色，多个以英文逗号(,)分割
     *
     * @param ids
     */
    static deleteByIds(ids: string) {
        return request({
            url: "/api/sysRole/delete",
            method: "post",
            data: {id: ids}
        });
    }
}

export default RoleAPI;
